//
//  UserDataManager.m
//  OrangeNote
//
//  Created by 刘崇龙 on 16/1/21.
//  Copyright © 2016年 崇龙. All rights reserved.
//

#import "UserDataManager.h"
#import "UserModel.h"
#import <AVOSCloud/AVOSCloud.h>
#import <UIKit/UIKit.h>

@implementation UserDataManager

// 用户注册
- (void)userRigstWithUserModel:(UserModel *)userModel{
    AVUser *user = [AVUser user];
    user.username = userModel.username;
    user.password = userModel.password;
    user.email = userModel.email;
    
    
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        if (succeeded) {
            NSLog(@"注册成功!");
            
            [self userInsertImage:[UIImage imageNamed:@"Icon-60@2x.jpg"] andUsername:user.username];
            
            self.registSuccessBlock();
        }else{
            
            switch (error.code) {
                case 125:
                    self.registFailedBlock(@"邮箱格式错误");
                    break;
                case 200:
                    self.registFailedBlock(@"没有提供用户名，或者用户名为空");
                    break;
                case 201:
                    self.registFailedBlock(@"没有提供密码，或者密码为空");
                    break;
                case 202:
                    self.registFailedBlock(@"用户名已被注册");
                    break;
                case 203:
                    self.registFailedBlock(@"电子邮箱地址已经被占用");
                    break;
                case 204:
                    self.registFailedBlock(@"没有提供电子邮箱地址");
                    break;
                case 217:
                    self.registFailedBlock(@"无效的用户名，不允许空白用户名");
                    break;
                case 218:
                    self.registFailedBlock(@"无效的密码，不允许空白密码");
                    break;
                    
                default:
                    NSLog(@"注册失败!%@",error);
                    break;
            }
        }
    }];
}

// 用户登陆
- (void)userLoginWithUsername:(NSString *)username andPassword:(NSString *)password{
    [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
        if (username != nil) {
            if ([user.username isEqualToString:username] && [user.password isEqualToString:password]) {
                NSLog(@"登陆成功!");
                
                // 每当用户成功注册或第一次成功登录后，就在本地磁盘中缓存下这个用户对象，供下次调用：
                AVUser *currentUser = [AVUser currentUser];
                if (currentUser != nil) {
                    // 允许用户使用应用
                } else {
                    
                    //缓存用户对象为空时，可打开用户注册界面…
                }
                
                self.loginSuccessBlock();
                
                
                
                
//                NSLog(@"当前登陆的账号:%@",currentUser.username);
                
            }else{
//                AVUser *currentUser = [AVUser currentUser];
//                NSLog(@"当前登陆的账号:%@",currentUser.username);
                switch (error.code) {
                    case 200:
                        self.loginFailedBlock(@"没有提供用户名，或者用户名为空");
                        break;
                    case 201:
                        self.loginFailedBlock(@"没有提供密码，或者密码为空");
                        break;
                    case 210:
                        self.loginFailedBlock(@"用户名和密码不匹配");
                        break;
                    case 211:
                        self.loginFailedBlock(@"找不到用户");
                        break;
                    case 217:
                        self.loginFailedBlock(@"无效的用户名，不允许空白用户名");
                        break;
                    case 218:
                        self.loginFailedBlock(@"无效的密码，不允许空白密码");
                        break;
                        
                    default:
                        
                        NSLog(@"登录失败!%@",error);
                        break;
                }
                
                
            }
        }else{
            
            NSLog(@"未找到用户!%@",error);
        }
    }];
}

// 为用户注册时为其创建UserInfo  插入placeholderImage  修改时 调用修改头像
- (void)userInsertImage:(UIImage *)image andUsername:(NSString *)username{
    
    AVObject *userInfo = [AVObject objectWithClassName:@"UserInfo"];
    
    NSData *imgData = UIImageJPEGRepresentation(image, 1);
    AVFile *img = [AVFile fileWithData:imgData];
    userInfo[@"image"] = img;
    
    [userInfo setObject:username forKey:@"username"];
    
    [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"用户头像插入成功!");
        }else{
            NSLog(@"用户头像插入失败!%@",error);
        }
    }];
}



// 修改用户头像
- (void)userUpdateUserImage:(UIImage *)image{
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser) {
        
        NSData *data = UIImageJPEGRepresentation(image, 1);
        AVFile *file = [AVFile fileWithData:data];
        
        AVQuery *query = [AVQuery queryWithClassName:@"UserInfo"];
        [query whereKey:@"username" equalTo:currentUser.username];
        
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            
            if (!error) {
                
                [objects.firstObject setObject:file forKey:@"image"];
                [objects.firstObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (succeeded) {
                        NSLog(@"修改头像成功!");
                        
                    }else{
                        NSLog(@"修改头像失败!%@",error);
                    }
                }];
                
            }else{
                NSLog(@"出错了!!%@",error);
            }
        }];
    }else{
        // 跳登陆
        NSLog(@"请先登陆!");
    }
}

- (void)selectUserImageWithUsername:(NSString *)username{
    AVQuery *query = [AVQuery queryWithClassName:@"UserInfo"];
    [query whereKey:@"username" equalTo:username];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        UIImage *img = [UIImage imageWithData:[objects.firstObject[@"image"] getData]];
        
        
        self.sendImage(img);
        
        
    }];
}


@end
